const appInstance = getApp()
var url = appInstance.globalData.url;
var Iv = '';
var EncryptedData = '';
var NickName = '';
var AvatarUrl = '';
var a;
var Number = ''

Page({
  data: {
    //判断小程序的API，回调，参数，组件等是否在当前版本可用。
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    show: false
  },
  onClose() {
    this.setData({
      show: false
    });
  },
  //获取手机号
  getPhoneNumber(e) {
    Iv = e.detail.iv;
    // console.log(Iv)
    EncryptedData = e.detail.encryptedData;
    // console.log(EncryptedData)
    this.ONnumber();
    //登录2
    wx.login({
      success(res) {
        wx.request({
          url: url + '/login/3rdWeChat',
          method: 'POST',
          data: {
            source: 'wechat',
            open_id: appInstance.globalData.OpenId,
            name: wx.getStorageSync('nickName'),
            avatar: wx.getStorageSync('avatarUl'),
            mobile: Number
          },
          header: {
            'content-type': 'application/x-www-form-urlencoded' // 默认值
          },
          dataType: 'json',
          success(res) {
            console.log('11111111',res.data.data.mobile)
            console.log(res.data.data.pp_token)
            wx.setStorageSync("to_ken", res.data.data.pp_token)
            // this.setData({
            //   show:false
            // })
            if (wx.getStorageSync("to_ken")) {
              // console.log(wx.getStorageSync("to_ken"))
              wx.navigateTo({
                url: '../my/my',
              })
            }
            wx.navigateTo({
              url: '../my/my',
            })
            // wx.switchTab({
            //   url: '../my/my',
            // })

          }
        })
      }

    })
  },
  //获取手机号方法
  ONnumber() {
    // var that = this;
    wx.request({
      url:  + '/login/checkSession',
      method: "POST",
      data: {
        iv: Iv,
        data: EncryptedData,
        session_key: wx.getStorageSync('session_key')
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      dataType: 'json',
      success(res) {
        console.log(res.data.data)
        Number = res.data.data
        // console.log(Number)
      }
    })
  },
  onLoad: function() {
  

  },
  onGotUserInfo(e) {
    wx.setStorageSync("nickName", e.detail.userInfo.nickName)
    wx.setStorageSync("avatarUrl", e.detail.userInfo.avatarUrl)
  },

  showToast() {
    this.setData({
      show: true
    })
  }



})
module.exports = {
  //获取手机号
  getPhoneNumber(e) {
    Iv = e.detail.iv;
    // console.log(Iv)
    EncryptedData = e.detail.encryptedData;
    // console.log(EncryptedData)
    this.ONnumber();
    //登录2
    wx.login({
      success(res) {
        wx.request({
          url: url + '/login/3rdWeChat',
          method: 'POST',
          data: {
            source: 'wechat',
            open_id: appInstance.globalData.OpenId,
            name: wx.getStorageSync('nickName'),
            avatar: wx.getStorageSync('avatarUl'),
            mobile: Number
          },
          header: {
            'content-type': 'application/x-www-form-urlencoded' // 默认值
          },
          dataType: 'json',
          success(res) {
            console.log(res.data.data.mobile)
            wx.setStorageSync("mobile", res.data.data.mobile)
            console.log(res.data.data.pp_token)
            wx.setStorageSync("to_ken", res.data.data.pp_token)
            // this.setData({
            //   show:false
            // })
            if (wx.getStorageSync("to_ken")) {
              // console.log(wx.getStorageSync("to_ken"))
              wx.navigateTo({
                url: '../my/my',
              })
            }
            wx.navigateTo({
              url: '../my/my',
            })
            // wx.switchTab({
            //   url: '../my/my',
            // })

          }
        })
      }

    })
  },
  //获取手机号方法
  ONnumber() {
    // var that = this;
    wx.request({
      url: url + '/login/checkSession',
      method: "POST",
      data: {
        iv: Iv,
        data: EncryptedData,
        session_key: wx.getStorageSync('session_key')
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      dataType: 'json',
      success(res) {
        // console.log(res.data.data.mobile)
        Number = res.data.data
        // console.log(Number)
      }
    })
  }
}